package com.djmw.mouse.web.jobsearch;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Validator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springside.modules.web.Servlets;

import com.djmw.mouse.entity.Company;
import com.djmw.mouse.entity.Position;
import com.djmw.mouse.service.CompanyService;
import com.djmw.mouse.service.PositionService;
import com.google.common.collect.Maps;

/**
 * 职位搜索的Controller, 使用Restful风格的Urls:
 * 
 * 根据关键字搜索: GET /jobsearch/keyword <br>
 * 高级搜索： GET /jobsearch/advanced <br>
 * 
 * @author michael
 */

@Controller
@RequestMapping(value = "/jobsearch")
public class JobSearchController {

    private static final int PAGE_SIZE = 25;

    private static Map<String, String> sortTypes = Maps.newLinkedHashMap();
    static {
        sortTypes.put("refreshDate", "更新日期");
        sortTypes.put("publishDate", "发布日期");
    }

    @Autowired
    private PositionService positionService;
    @Autowired
    private CompanyService companyService;
    // @Autowired
    // private DepartmentService departmentService;
    // @Autowired
    // private HrAccountService hrAccountService;
    // @Autowired
    // private PositionHistoryService positionHistoryService;
    @Autowired
    private Validator validator;

    @RequestMapping(value = "keyword")
    public String keyword(@RequestParam(value = "sortType", defaultValue = "refreshDate") String sortType,
            @RequestParam(value = "sortDir", defaultValue = "a") String sortDir,
            @RequestParam(value = "page", defaultValue = "1") int pageNumber,
            /*
             * @RequestParam(value = "type", defaultValue = "p") String type,
             * @RequestParam(value = "keyword") String keyword,
             * @RequestParam(value = "area") String area,
             */
            Model model, ServletRequest request) {
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        decode(searchParams);
        Page<Position> positions = positionService.getPosition(searchParams, pageNumber, PAGE_SIZE, sortType, sortDir);
        model.addAttribute("positions", positions);
        model.addAttribute("sortType", sortType);
        model.addAttribute("sortTypes", sortTypes);
        // 将搜索条件编码成字符串，用于排序，分页的URL
        encode(searchParams);
        model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
        return "jobsearch/keyword-result-list";
    }

    private void encode(Map<String, Object> searchParams) {
        for (String key : searchParams.keySet()) {
            if (key.indexOf(".") >= 0) {
                Object value = searchParams.get(key);
                searchParams.remove(key);
                key = key.replaceAll(".", "__");
                searchParams.put(key, value);
            }
        }
    }

    private void decode(Map<String, Object> searchParams) {
        for (String key : searchParams.keySet()) {
            if (key.indexOf("__") >= 0) {
                Object value = searchParams.get(key);
                searchParams.remove(key);
                key = key.replaceAll("__", ".");
                searchParams.put(key, value);
            }
        }
    }

    @RequestMapping(value = "position/{id}")
    public String position(@PathVariable("id") Long id, Model model) {
        Position position = positionService.getPosition(id);
        model.addAttribute("position", position);
        return "jobsearch/view-position";
    }

    @RequestMapping(value = "company/{id}")
    public String company(@PathVariable("id") Long companyId,
            @RequestParam(value = "sortType", defaultValue = "refreshDate") String sortType,
            @RequestParam(value = "sortDir", defaultValue = "d") String sortDir,
            @RequestParam(value = "page", defaultValue = "1") int pageNumber,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize, Model model, ServletRequest request) {
        Company company = companyService.getCompany(companyId);
        model.addAttribute("company", company);
        Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
        Page<Position> positions = positionService.getCompanyPosition(companyId, searchParams, pageNumber, pageSize,
                sortType, sortDir);
        model.addAttribute("positions", positions);
        model.addAttribute("sortType", sortType);
        model.addAttribute("sortTypes", sortTypes);
        return "jobsearch/view-company";
    }
}
